package com.example.yunhailutong.zhongtan.app;


import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.view.WindowManager;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.example.yunhailutong.zhongtan.activity.MainActivity;
import com.example.yunhailutong.zhongtan.util.SharedPreferencesUtils;
import com.tencent.bugly.Bugly;


import java.io.File;

import cn.jpush.android.api.JPushInterface;


/**
 * Created by victor on 2017 8 24 10点16分
 * 保存app里面的全局变量  和全局方法
 */
public class App extends Application {

    public static RequestQueue mQueue;
    public static String city = "广州市";
    public static double lat = 23.1283793844, lng = 113.2967021103;
    public static Context context;
    public static boolean isLogin = false;
    public static String number = "13811111111";
    public static String oldPhoneNumber ="";
    public static String id = "1022";
    public static String token  = "";
    public static String iemi  = "866815030275576";


    @Override
    public void onCreate() {
        super.onCreate();
        //图片加载框架
        //添加volley队列
        mQueue = Volley.newRequestQueue(this);
       // Fresco.initialize(this);
        context = this;
        isLogin = (boolean) SharedPreferencesUtils.getParam(this, "islogin", false);
        number = (String) SharedPreferencesUtils.getParam(this, "number", "");
        id = (String) SharedPreferencesUtils.getParam(this, "id", "");
        token = (String) SharedPreferencesUtils.getParam(this, "token", "");
        oldPhoneNumber = (String) SharedPreferencesUtils.getParam(this, "oldNumber", "");

        //initpush();
//        if (isLogin) {
//            Intent intent =new Intent(getApplicationContext(), MainActivity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//            startActivity(intent);
//        }
        initpush();
        //initDownload();
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
       // SDKInitializer.initialize(getApplicationContext());
//        String a =JPushInterface.getRegistrationID(this);
//        System.out.print(a);


    }
    public static void initpush() {
        JPushInterface.setDebugMode(true);
        JPushInterface.setAlias(context, 0, App.number);
        JPushInterface.init(context);
    }
    /**
     * 添加volley request到队列
     *
     * @param request
     * @return
     */
    public static Request addVolleyRequest(@NonNull Request request) {
        return mQueue.add(request);
    }

//    public static void initpush() {
//        JPushInterface.setDebugMode(true);
//        JPushInterface.setAlias(context, 0, number);
//        JPushInterface.init(context);
//    }
    // 提示用户该请求权限的弹出框
    private void showDialogTipUserRequestPermission() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("存储权限不可用")
                .setMessage("请在-应用设置-权限-中，允许医养结合使用存储权限来保存用户数据")
                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        goToAppSetting();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).setCancelable(false);
        AlertDialog alertDialog = builder.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();
    }

    // 跳转到当前应用的设置界面
    private void goToAppSetting() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

//    private void initDownload() {
//        // 1、创建Builder
//        FileDownloadConfiguration.Builder builder = new FileDownloadConfiguration.Builder(this);
//        // 2.配置Builder
//        // 配置下载文件保存的文件夹
//        builder.configFileDownloadDir(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
//                "FileDownloader");
//        // 配置同时下载任务数量，如果不配置默认为2
//        builder.configDownloadTaskSize(3);
//        // 配置失败时尝试重试的次数，如果不配置默认为0不尝试
//        builder.configRetryDownloadTimes(5);
//        // 开启调试模式，方便查看日志等调试相关，如果不配置默认不开启
//        builder.configDebugMode(true);
//        // 配置连接网络超时时间，如果不配置默认为15秒
//        builder.configConnectTimeout(25000);// 25秒
//        // 3、使用配置文件初始化FileDownloader
//        FileDownloadConfiguration configuration = builder.build();
//        FileDownloader.init(configuration);
//    }
}
